<?php

namespace App\Http\Controllers\Manager;

use App\Events\ManagerMoneyChangeEvent;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Models\Advert;
use App\Models\AdvertClickLog;
use App\Models\AdvertPrice;
use App\Models\ApiLog;
use App\Models\ServerSpellMall;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class AdController extends Controller
{
    public $prefix='manager.ad';

    public function index(){
        return view($this->prefix.'.index');
    }

    public function list(){
        $user=Auth::user();
        $ads=$user->ads;
        foreach ($ads as $ad){
            $prices=[];
            for ($i=1;$i<=14;$i++){
                $date = Carbon::parse("+$i days")->toDateString();
                $prices[$date]=0;
            }
            $ad->prices=$prices;
        }

        return ['ads'=>$ads];

    }
    public function plist(){
        $pList=[];
        for ($i=1;$i<=14;$i++){
            $date = Carbon::parse("+$i days")->toDateString();
            $pList[$date]=AdvertPrice::where('user_id',Auth::id())->where('date',$date)->get();
        }

        $sevendays = Carbon::parse('+1 days')->toDateString();

        return ['pList'=>$pList,'t'=>$sevendays];
    }
    public function add(Request $request){
        $title=$request->post('title');
        $url=$request->post('url');
        $img=$request->post('img');

        $ad=new Advert();
        $ad->user_id=Auth::id();
        $ad->title=$title;
        $ad->url=$url;
        $ad->img=$img;
//        $ad->status=1;
        $ad->save();

        return $ad;
    }
    public function del(Request $request){
        $id=$request->get('id');
        $ad=Advert::find($id);

        if ($ad && $ad->user_id==Auth::id()){
            AdvertPrice::where('advert_id',$id)->delete();
            $ad->delete();
            return 1;
        }else{
            return -1;
        }
    }

    public function save(Request $request){
        $id=$request->get('id');
        $date=$request->get('date');
        $price=$request->get('price');

        $now = Carbon::now()->toDateString();
        if ($date<=$now){
            return ['err'=>-1,'msg'=>'错误的时间'];
        }

        $user=Auth::user();

        $ad=Advert::find($id);

        if (!$ad || $ad->status !=1 || $ad->user_id !=$user->id){
            return ['err'=>-1,'msg'=>'错误'];
        }

        $item=AdvertPrice::where('advert_id',$id)->where('date',$date)->first();

        if (!$item){
            if ($user->remain_money<$price){
                return ['err'=>-1,'msg'=>'积分不足'];
            }
            $user->remain_money-=$price;
            $user->save();
            event(new ManagerMoneyChangeEvent($user->id,0, 0,7,2,$price,0,$user->remain_money,'广告投放'));
            //添加
            $item=new AdvertPrice();
            $item->user_id=$user->id;
            $item->date=$date;
            $item->advert_id=$id;
        }else{
            if ($item->user_id !=$user->id){
                return ['err'=>-1,'msg'=>'error'];
            }
            if ($item->price==$price){
                return ['err'=>0,'data'=>$user->remain_money];
            }
            $change= $price-$item->price;
            if ($change>0 && $user->remain_money<$change){
                return ['err'=>-1,'msg'=>'积分不足'];
            }
            $user->remain_money-=$change;
            $user->save();
            event(new ManagerMoneyChangeEvent($user->id,0, 0,7,$change>0?2:1,abs($change),0,$user->remain_money,'广告投放'));

        }
        $item->price=$price;
        $item->save();
        return ['err'=>0,'data'=>$user->remain_money];
    }

    public function getLog(Request $request){
        $date=$request->get('date');

        $logList=AdvertPrice::where('price','>',0)->where('date',$date)
            ->orderBy('price','desc')
            ->orderByRaw('RAND()')->with('advert')->get();
        return ['logList'=>$logList];
    }

    public function getClickInfo(Request $request){
        $id=$request->get('id');
        $clickList= AdvertClickLog::where('advert_id',$id)->orderby('id','desc')->take(100)->get();
        return ['clickList'=>$clickList];
    }
}
